package com.lineage.server.clientpackets;

import com.add.NewAutoPractice;
import com.eric.gui.J_Main;
import com.lineage.DatabaseFactory;
import com.lineage.config.Config;
import com.lineage.config.ConfigAi;
import com.lineage.config.ConfigAlt;
import com.lineage.config.ConfigGuaji;
import com.lineage.config.ConfigOther;
import com.lineage.config.ConfigRecord;
import com.lineage.data.event.BroadcastSet;
import com.lineage.data.event.QuestMobSet;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.BroadcastController;
import com.lineage.server.command.GMCommands;
import com.lineage.server.datatables.CommandsTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.datatables.ServerQuestMobTable;
import com.lineage.server.datatables.lock.AccountReading;
import com.lineage.server.datatables.lock.CharBookReading;
import com.lineage.server.datatables.lock.ClanAllianceReading;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Alliance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_CharTitle;
import com.lineage.server.serverpackets.S_Chat;
import com.lineage.server.serverpackets.S_ChatClan;
import com.lineage.server.serverpackets.S_ChatClanAlliance;
import com.lineage.server.serverpackets.S_ChatClanUnion;
import com.lineage.server.serverpackets.S_ChatParty;
import com.lineage.server.serverpackets.S_ChatParty2;
import com.lineage.server.serverpackets.S_ChatShouting;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_Disconnect;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_NpcChat;
import com.lineage.server.serverpackets.S_NpcChatShouting;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.S_WhoHero;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Command;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.Random;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Chat extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Chat.class);
  
  private static final String _check_pwd = "abcdefghijklmnopqrstuvwxyz0123456789!_=+-?.#";
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      int chatType = readC();
      String chatText = readS();
      if (chatText != null && chatText.length() > 52)
        chatText = chatText.substring(0, 52); 
      boolean isStop = false;
      boolean errMessage = false;
      if (pc.hasSkillEffect(64) && !pc.isGm())
        isStop = true; 
      if (pc.hasSkillEffect(161) && !pc.isGm())
        isStop = true; 
      if (pc.hasSkillEffect(1007) && !pc.isGm())
        isStop = true; 
      if (pc.hasSkillEffect(4002)) {
        isStop = true;
        errMessage = true;
      } 
      if (pc.hasSkillEffect(7004)) {
        isStop = true;
        errMessage = true;
      } 
      if (pc.hasSkillEffect(9990)) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前受到监禁状态中无法正常说话。"));
      } else {
        if ((pc.getMapId() == 5153 || pc.getMapId() == 5154) && !pc.isGm())
          isStop = true; 
        if (isStop) {
          if (errMessage)
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(242)); 
        } else {
          switch (chatType) {
            case 0:
              if (pc.is_retitle()) {
                re_title(pc, chatText.trim());
                return;
              } 
              if (pc.is_repass() != 0) {
                re_repass(pc, chatText.trim());
                return;
              } 
              if (pc.IsKeyInEnemy()) {
                KeyInEnemyName(pc, chatText.trim());
                return;
              } 
              if (pc.IsKeyOutEnemy()) {
                KeyOutEnemyName(pc, chatText.trim());
                return;
              } 
              if (pc.IsBadKeyInEnemy()) {
                BadKeyInEnemyName(pc, chatText.trim());
                return;
              } 
              if (pc.IsBadKeyOutEnemy()) {
                BadKeyOutEnemyName(pc, chatText.trim());
                return;
              } 
              if (pc.hasSkillEffect(8008)) {
                pc.killSkillEffectTimer(8008);
                check_broadcast(pc, chatText);
                return;
              } 
              if (chatText.startsWith(String.valueOf(pc.getnewaicount_2())) && pc.hasSkillEffect(7954)) {
                pc.killSkillEffectTimer(7954);
                pc.setnewaicount_2(0);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg3));
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg4));
                int time = ConfigAi.aitimelast + Random.nextInt(ConfigAi.aitimeran) + 1;
                pc.setSkillEffect(7950, time * 1000);
              } else if (!chatText.startsWith(String.valueOf(pc.getnewaicount_2())) && pc.hasSkillEffect(7954) && 
                pc.getInputError() == 0) {
                pc.setInputError(pc.getInputError() + 1);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg7));
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg5));
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg6));
              } else if (!chatText.startsWith(String.valueOf(pc.getnewaicount_2())) && pc.hasSkillEffect(7954) && 
                pc.getInputError() == 1) {
                pc.setInputError(pc.getInputError() + 1);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg7));
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg5));
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(ConfigAi.msg6));
              } else if (pc.getInputError() == 2 && pc.hasSkillEffect(7954)) {
                pc.setInputError(0);
                pc.saveInventory();
                pc.sendPackets((ServerBasePacket)new S_Disconnect());
                _log.info(String.format("玩家 : %s 因输入错误未通过外挂侦测，已强制切断其连线", new Object[] { pc.getName() }));
              } 
              if (pc.hasSkillEffect(95133) && chatText.startsWith(chatText)) {
                pc.killSkillEffectTimer(95133);
                CharBookReading.get().deleteBookmark(pc, chatText);
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("已删除此名称座标.请重新登入游戏"));
                return;
              } 
              if (pc.isItemName()) {
                ItemName(pc, chatText);
                pc.setItemName(false);
                return;
              } 
              if (chatText.startsWith(ConfigGuaji.guaji)) {
                guaji(pc);
                return;
              } 
              if (chatText.startsWith("解卡点")) {
                if (pc.isParalyzed() || pc.isSleeped())
                  return; 
                if (pc.isParalyzedX())
                  return; 
                if (pc.isDead())
                  return; 
                if (pc.isInvisble())
                  return; 
                if (pc.hasSkillEffect(55889)) {
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage("无法连续使用该功能"));
                  return;
                } 
                if (pc.hasSkillEffect(76001)) {
                  pc.sendPackets((ServerBasePacket)new S_SystemMessage("请不要按太快.."));
                  return;
                } 
                if (pc.hasSkillEffect(4000))
                  return; 
                pc.setSkillEffect(55889, 30000);
                pc.set_unfreezingTime(5);
                return;
              } 
              if (chatText.startsWith("解错位")) {
                if (pc.isParalyzed() || pc.isSleeped())
                  return; 
                if (pc.isParalyzedX())
                  return; 
                if (pc.isDead())
                  return; 
                if (pc.isInvisble())
                  return; 
                if (pc.hasSkillEffect(55889)) {
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage("无法连续使用该功能"));
                  return;
                } 
                if (pc.hasSkillEffect(76001)) {
                  pc.sendPackets((ServerBasePacket)new S_SystemMessage("请不要按太快.."));
                  return;
                } 
                pc.setSkillEffect(55889, 30000);
                pc.set_misslocTime(3);
                return;
              } 
              if (QuestMobSet.START && chatText.equalsIgnoreCase(".任务")) {
                ServerQuestMobTable.get().getQuestMobNote(pc);
                return;
              } 
              if (Config.GUI)
                J_Main.getInstance().addNormalChat(pc.getName(), chatText); 
              chatType_0(pc, chatText);
              if (ConfigAlt.GM_OVERHEARD0) {
                Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
                while (iterator.hasNext()) {
                  L1Object visible = (L1Object)iterator.next();
                  if (visible instanceof L1PcInstance) {
                    L1PcInstance GM = (L1PcInstance)visible;
                    if (GM.isGm() && pc.getId() != GM.getId() && GM.getChatListenNormal().booleanValue())
                      GM.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fW【一般】", " ( " + pc.getName() + " ) :【 ", 
                            String.valueOf(String.valueOf(chatText)) + " 】")); 
                  } 
                } 
              } 
              break;
            case 2:
              if (ConfigOther.SET_SHOUT_TIME > 0 && !pc.isGm()) {
                Calendar cal = Calendar.getInstance();
                long time = cal.getTimeInMillis() / 1000L;
                if (time - pc.get_global_time() < ConfigOther.SET_SHOUT_TIME) {
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aG系统：为了频道品质，请稍等一下再喊。"));
                  return;
                } 
                pc.set_global_time(time);
              } 
//              if (isCommandChat(pc, chatText).booleanValue())
//                break; 
              if (Config.GUI)
                J_Main.getInstance().addNormalChat(pc.getName(), chatText); 
              chatType_2(pc, chatText);
              if (ConfigAlt.GM_OVERHEARD0) {
                Iterator<L1PcInstance> iterator2 = World.get().getAllPlayers().iterator();
                while (iterator2.hasNext()) {
                  L1Object visible = (L1Object)iterator2.next();
                  if (visible instanceof L1PcInstance) {
                    L1PcInstance GM = (L1PcInstance)visible;
                    if (GM.isGm() && pc.getId() != GM.getId() && GM.getChatListenShout().booleanValue())
                      GM.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fV【大喊】", " ( " + pc.getName() + " ) :【 ", 
                            String.valueOf(String.valueOf(chatText)) + " 】")); 
                  } 
                } 
              } 
              break;
            case 4:
//              if (isCommandChat(pc, chatText).booleanValue())
//                break; 
              if (Config.GUI)
                J_Main.getInstance().addClanChat(pc.getName(), chatText); 
              chatType_4(pc, chatText);
              if (ConfigAlt.GM_OVERHEARD4) {
                Iterator<L1PcInstance> iterator3 = World.get().getAllPlayers().iterator();
                while (iterator3.hasNext()) {
                  L1Object visible = (L1Object)iterator3.next();
                  if (visible instanceof L1PcInstance) {
                    L1PcInstance GM = (L1PcInstance)visible;
                    if (GM.isGm() && pc.getId() != GM.getId() && GM.getChatListenClan().booleanValue())
                      GM.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fV【血盟】", " ( " + pc.getName() + " ) :【 ", 
                            String.valueOf(String.valueOf(chatText)) + " 】")); 
                  } 
                } 
              } 
              break;
            case 11:
//              if (isCommandChat(pc, chatText).booleanValue())
//                break; 
              if (Config.GUI)
                J_Main.getInstance().addTeamChat(pc.getName(), chatText); 
              chatType_11(pc, chatText);
              if (ConfigAlt.GM_OVERHEARD11) {
                Iterator<L1PcInstance> iterator4 = World.get().getAllPlayers().iterator();
                while (iterator4.hasNext()) {
                  L1Object visible = (L1Object)iterator4.next();
                  if (visible instanceof L1PcInstance) {
                    L1PcInstance GM = (L1PcInstance)visible;
                    if (GM.isGm() && pc.getId() != GM.getId() && GM.getChatListenParty().booleanValue())
                      GM.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fV【队伍】", " ( " + pc.getName() + " ) :【 ", 
                            String.valueOf(String.valueOf(chatText)) + " 】")); 
                  } 
                } 
              } 
              break;
            case 13:
//              if (isCommandChat(pc, chatText).booleanValue())
//                break; 
              if (Config.GUI)
                J_Main.getInstance().addClanChat(pc.getName(), chatText); 
              chatType_13(pc, chatText);
              if (ConfigAlt.GM_OVERHEARD4) {
                Iterator<L1PcInstance> iterator5 = World.get().getAllPlayers().iterator();
                while (iterator5.hasNext()) {
                  L1Object visible = (L1Object)iterator5.next();
                  if (visible instanceof L1PcInstance) {
                    L1PcInstance GM = (L1PcInstance)visible;
                    if (GM.isGm() && pc.getId() != GM.getId() && GM.getChatListenAlliance().booleanValue())
                      GM.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fV【联盟】", " ( " + pc.getName() + " ) :【 ", 
                            String.valueOf(String.valueOf(chatText)) + " 】")); 
                  } 
                } 
              } 
              break;
            case 15:
//              if (isCommandChat(pc, chatText).booleanValue())
//                break; 
              chatType_15(pc, chatText);
              break;
            case 14:
//              if (isCommandChat(pc, chatText).booleanValue())
//                break; 
              if (Config.GUI)
                J_Main.getInstance().addTeamChat(pc.getName(), chatText); 
              chatType_14(pc, chatText);
              if (ConfigAlt.GM_OVERHEARD0) {
                Iterator<L1PcInstance> iterator6 = World.get().getAllPlayers().iterator();
                while (iterator6.hasNext()) {
                  L1Object visible = (L1Object)iterator6.next();
                  if (visible instanceof L1PcInstance) {
                    L1PcInstance GM = (L1PcInstance)visible;
                    if (GM.isGm() && pc.getId() != GM.getId() && GM.getChatListenParty().booleanValue())
                      GM.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fV【队伍】", " ( " + pc.getName() + " ) :【 ", 
                            String.valueOf(String.valueOf(chatText)) + " 】")); 
                  } 
                } 
              } 
              break;
          } 
          if (!pc.isGm()) {
            pc.checkChatInterval();
            return;
          } 
          return;
        } 
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private void re_repass(L1PcInstance pc, String password) {
    try {
      boolean iserr;
      int i;
      switch (pc.is_repass()) {
        case 1:
          if (!pc.getNetConnection().getAccount().get_password().equals(password)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1744));
            return;
          } 
          pc.repass(2);
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "y_pass_01", new String[] { "请输入您的新密码" }));
          break;
        case 2:
          iserr = false;
          i = 0;
          while (i < password.length()) {
            String ch = password.substring(i, i + 1);
            if (!"abcdefghijklmnopqrstuvwxyz0123456789!_=+-?.#".contains(ch.toLowerCase())) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1742));
              iserr = true;
              break;
            } 
            i++;
          } 
          if (password.length() > 13) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "密码长度过长"));
            iserr = true;
          } 
          if (password.length() < 3) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "密码长度过长"));
            iserr = true;
          } 
          if (iserr)
            return; 
          pc.setText(password);
          pc.repass(3);
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "y_pass_01", new String[] { "请确认您的新密码" }));
          break;
        case 3:
          if (!pc.getText().equals(password)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1982));
            return;
          } 
          pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
          pc.getInventory().consumeItem(49538, 1L);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1985));
          AccountReading.get().updatePwd(pc.getAccountName(), password);
          pc.setText(null);
          pc.repass(0);
          RecordTable.get().recordPcChangePassWord(pc.getId(), pc.getAccountName(), pc.getName(), password, 
              pc.getIp());
          break;
      } 
    } catch (Exception e) {
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(45));
      pc.setText(null);
      pc.repass(0);
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void re_title(L1PcInstance pc, String chatText) {
    try {
      String newchatText = chatText.trim();
      if (newchatText.isEmpty() || newchatText.length() <= 0) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU请输入封号内容"));
        return;
      } 
      int length = Config.LOGINS_TO_AUTOENTICATION ? 18 : 13;
      if ((newchatText.getBytes()).length > length) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU封号长度过长"));
        return;
      } 
      StringBuilder title = new StringBuilder();
      title.append(newchatText);
      pc.setTitle(title.toString());
      pc.sendPacketsAll((ServerBasePacket)new S_CharTitle(pc.getId(), title));
      pc.save();
      pc.retitle(false);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU封号变更完成"));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void chatType_15(L1PcInstance pc, String chatText) {
    if (pc.getClanid() != 0) {
      L1Clan clan = pc.getClan();
      if (clan == null)
        return; 
      L1Alliance alliance = ClanAllianceReading.get().getAlliance(clan.getClanId());
      if (alliance == null)
        return; 
      S_ChatClanAlliance chatpacket = new S_ChatClanAlliance(pc, clan.getClanName(), chatText);
      alliance.sendPacketsAll(pc.getName(), (ServerBasePacket)chatpacket);
    } 
  }
  
  private void chatType_14(L1PcInstance pc, String chatText) {
    if (pc.isInChatParty()) {
      S_ChatParty2 chatpacket = new S_ChatParty2(pc, chatText);
      L1PcInstance[] partyMembers = pc.getChatParty().getMembers();
      L1PcInstance[] array;
      int length = (array = partyMembers).length;
      int i = 0;
      while (i < length) {
        L1PcInstance listner = array[i];
        System.out.println(listner.isBadInEnemyList(pc.getName()));
        if (!listner.isBadInEnemyList(pc.getName()) && !listner.getExcludingList().contains(pc.getName()))
          listner.sendPackets((ServerBasePacket)chatpacket); 
        i++;
      } 
      if (ConfigRecord.LOGGING_CHAT_CHAT_PARTY)
        RecordTable.get().recordeTalk("群聊", pc.getName(), pc.getClanname(), null, chatText); 
    } 
  }
  
  private void chatType_13(L1PcInstance pc, String chatText) {
    if (pc.getClanid() != 0) {
      S_ChatClanUnion chatpacket;
      L1PcInstance[] clanMembers, array;
      int length, i;
      L1Clan clan = WorldClan.get().getClan(pc.getClanname());
      if (clan == null)
        return; 
      switch (pc.getClanRank()) {
        case 3:
        case 4:
        case 6:
        case 9:
        case 10:
          chatpacket = new S_ChatClanUnion(pc, chatText);
          clanMembers = clan.getOnlineClanMember();
          length = (array = clanMembers).length;
          i = 0;
          while (i < length) {
            L1PcInstance listner = array[i];
            if (!listner.isBadInEnemyList(pc.getName()) && !listner.getExcludingList().contains(pc.getName()))
              switch (listner.getClanRank()) {
                case 3:
                case 4:
                case 6:
                case 9:
                case 10:
                  listner.sendPackets((ServerBasePacket)chatpacket);
                  break;
              }  
            i++;
          } 
          if (ConfigRecord.LOGGING_CHAT_COMBINED)
            RecordTable.get().recordeTalk("联盟", pc.getName(), pc.getClanname(), null, chatText); 
          break;
      } 
    } 
  }
  
  private void chatType_11(L1PcInstance pc, String chatText) {
    if (pc.isInParty()) {
      S_ChatParty chatpacket = new S_ChatParty(pc, chatText);
      ConcurrentHashMap<Integer, L1PcInstance> pcs = pc.getParty().partyUsers();
      if (pcs.isEmpty())
        return; 
      if (pcs.size() <= 0)
        return; 
      Iterator<L1PcInstance> iter = pcs.values().iterator();
      while (iter.hasNext()) {
        L1PcInstance listner = iter.next();
        if (!listner.isBadInEnemyList(pc.getName()) && !listner.getExcludingList().contains(pc.getName()))
          listner.sendPackets((ServerBasePacket)chatpacket); 
      } 
      if (ConfigRecord.LOGGING_CHAT_PARTY)
        RecordTable.get().recordeTalk("组队", pc.getName(), pc.getClanname(), null, chatText); 
    } 
  }
  
  private void chatType_4(L1PcInstance pc, String chatText) {
    if (pc.getClanid() != 0) {
      L1Clan clan = WorldClan.get().getClan(pc.getClanname());
      if (clan != null) {
        S_ChatClan chatpacket = new S_ChatClan(pc, chatText);
        L1PcInstance[] clanMembers = clan.getOnlineClanMember();
        L1PcInstance[] array;
        int length = (array = clanMembers).length;
        int i = 0;
        while (i < length) {
          L1PcInstance listner = array[i];
          if (!listner.isBadInEnemyList(pc.getName()) && !listner.getExcludingList().contains(pc.getName()))
            listner.sendPackets((ServerBasePacket)chatpacket); 
          i++;
        } 
        if (ConfigRecord.LOGGING_CHAT_CLAN)
          RecordTable.get().recordeTalk("盟频", pc.getName(), pc.getClanname(), null, chatText); 
      } 
    } 
  }
  
  private void chatType_2(L1PcInstance pc, String chatText) {
    if (pc.isGhost())
      return; 
    S_ChatShouting chatpacket = null;
    String name = pc.getName();
    if (pc.get_outChat() == null) {
      chatpacket = new S_ChatShouting(pc, chatText);
    } else {
      chatpacket = new S_ChatShouting((L1NpcInstance)pc.get_outChat(), chatText);
      name = pc.get_outChat().getNameId();
    } 
    pc.sendPackets((ServerBasePacket)chatpacket);
    Iterator<L1PcInstance> iterator = World.get().getVisiblePlayer((L1Object)pc, 50).iterator();
    while (iterator.hasNext()) {
      L1PcInstance listner = iterator.next();
      if (!listner.getExcludingList().contains(name) && !listner.isBadInEnemyList(pc.getName()) && 
        pc.get_showId() == listner.get_showId())
        listner.sendPackets((ServerBasePacket)chatpacket); 
    } 
    if (ConfigRecord.LOGGING_CHAT_SHOUT)
      RecordTable.get().recordeTalk("大喊", pc.getName(), pc.getClanname(), null, chatText); 
    doppelShouting(pc, chatText);
  }
  
  private void chatType_0(L1PcInstance pc, String chatText) {
    if (chatText.startsWith("sccommand")) {
      ArrayList<L1Command> list = new ArrayList<>();
      Iterator<L1Command> iterator = CommandsTable.get().getList().iterator();
      while (iterator.hasNext()) {
        L1Command command = iterator.next();
        if (pc == null) {
          if (!command.isSystem())
            continue; 
          list.add(command);
          continue;
        } 
        list.add(command);
      } 
      Iterator<L1Command> iterator2 = list.iterator();
      while (iterator2.hasNext()) {
        L1Command command = iterator2.next();
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, String.valueOf(String.valueOf(command.getName())) + ": " + command.get_note()));
      } 
      return;
    } 
    if (pc.isGhost() && !pc.isGm() && !pc.isMonitor())
      return; 
    if (pc.getAccessLevel() == 0 && pc.hasSkillEffect(51239) && chatText.startsWith(".")) {
      String cmd = chatText.substring(1);
      GMCommands.getInstance().handleCommands(pc, cmd);
      return;
    } 
    if ((pc.getAccessLevel() == 200 || pc.getAccessLevel() == 100) && chatText.startsWith(".")) {
      String cmd = chatText.substring(1);
      GMCommands.getInstance().handleCommands(pc, cmd);
      return;
    } 
    S_Chat chatpacket = null;
    String name = pc.getName();
    if (pc.get_outChat() == null) {
      chatpacket = new S_Chat(pc, chatText);
    } else {
      chatpacket = new S_Chat((L1NpcInstance)pc.get_outChat(), chatText);
      name = pc.get_outChat().getNameId();
    } 
    pc.sendPackets((ServerBasePacket)chatpacket);
    Iterator<L1PcInstance> iterator3 = World.get().getRecognizePlayer((L1Object)pc).iterator();
    while (iterator3.hasNext()) {
      L1PcInstance listner = iterator3.next();
      if (!listner.getExcludingList().contains(name) && !listner.isBadInEnemyList(pc.getName()) && 
        pc.get_showId() == listner.get_showId())
        listner.sendPackets((ServerBasePacket)chatpacket); 
    } 
    if (ConfigRecord.LOGGING_CHAT_NORMAL && !pc.hasSkillEffect(51239))
      RecordTable.get().recordeTalk("一般", pc.getName(), pc.getClanname(), null, chatText); 
    doppelGenerally(pc, chatText);
  }
  
  private void doppelGenerally(L1PcInstance pc, String chatText) {
    Iterator<L1Object> iterator = pc.getKnownObjects().iterator();
    while (iterator.hasNext()) {
      L1Object obj = iterator.next();
      if (obj instanceof L1MonsterInstance) {
        L1MonsterInstance mob = (L1MonsterInstance)obj;
        if (!mob.getNpcTemplate().is_doppel() || !mob.getName().equals(pc.getName()))
          continue; 
        mob.broadcastPacketX8((ServerBasePacket)new S_NpcChat((L1NpcInstance)mob, chatText));
      } 
    } 
  }
  
  private void doppelShouting(L1PcInstance pc, String chatText) {
    Iterator<L1Object> iterator = pc.getKnownObjects().iterator();
    while (iterator.hasNext()) {
      L1Object obj = iterator.next();
      if (obj instanceof L1MonsterInstance) {
        L1MonsterInstance mob = (L1MonsterInstance)obj;
        if (!mob.getNpcTemplate().is_doppel() || !mob.getName().equals(pc.getName()))
          continue; 
        mob.broadcastPacketX8((ServerBasePacket)new S_NpcChatShouting((L1NpcInstance)mob, chatText));
      } 
    } 
  }
  
  private void check_broadcast(L1PcInstance pc, String chatText) {
    try {
      if (chatText.isEmpty() || chatText.length() <= 0) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("请输入讯息内容。"));
        return;
      } 
      if (pc.isGm()) {
        if (chatText.equals("开启")) {
          BroadcastController.getInstance().setStop(false);
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("广播系统已开启。"));
          return;
        } 
        if (chatText.equals("关闭")) {
          BroadcastController.getInstance().setStop(true);
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("广播系统已关闭。"));
          return;
        } 
      } 
      if ((chatText.getBytes()).length > 50) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("广播讯息长度过长 (不能超过25个中文字)"));
        return;
      } 
      StringBuilder message = new StringBuilder();
      message.append("玩家【").append(pc.getName()).append("】对大家说 : ").append(chatText);
      L1ItemInstance item = pc.getInventory().checkItemX(BroadcastSet.ITEM_ID, 1L);
      if (item == null) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("不具有广播卡，因此无法发送讯息。"));
        return;
      } 
      if (BroadcastController.getInstance().requestWork(message.toString())) {
        pc.getInventory().removeItem(item, 1L);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("已成功发布广播讯息。"));
      } else {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前有太多等待讯息，请稍后再尝试一次。"));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void guaji(L1PcInstance pc) {
    try {
      int i = 0;
      String type1 = "";
      String type2 = "关闭";
      String type3 = "";
      if (pc.getgo_guajitele()) {
        type1 = "[已开启]";
      } else {
        type1 = "关闭";
      } 
      if (pc.get_fwgj() > 0)
        i = pc.get_fwgj(); 
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "guaiji", new String[] { type1, i+"","" }));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void Itemopen(L1PcInstance pc, String arg) {
    L1PcInstance target = World.get().getPlayer(arg);
    if (target != null) {
      List<L1ItemInstance> items = pc.getInventory().getItems();
      List<L1ItemInstance> itemsx = new CopyOnWriteArrayList<>();
      Iterator<L1ItemInstance> iterator = items.iterator();
      while (iterator.hasNext()) {
        L1ItemInstance item = iterator.next();
        if (item.isEquipped())
          itemsx.add(item); 
      } 
      target.sendPackets((ServerBasePacket)new S_WhoHero(target, 0, itemsx));
      String msg1 = "";
      String msg2 = "";
      String msg3 = "";
      String msg4 = "";
      String msg5 = "";
      String msg6 = "";
      String msg7 = "";
      String msg8 = "";
      String msg9 = "";
      String msg10 = "";
      String msg11 = "";
      int i = pc.getLevel();
      msg2 = String.valueOf(pc.getCurrentHp()) + " / " + pc.getMaxHp();
      msg3 = String.valueOf(pc.getCurrentMp()) + " / " + pc.getMaxMp();
      int j = pc.getAc();
      short s1 = pc.getStr();
      short s2 = pc.getDex();
      short s3 = pc.getInt();
      short s4 = pc.getCon();
      short s5 = pc.getWis();
      short s6 = pc.getCha();
      msg11 = String.valueOf(pc.getMr()) + " %";
      target.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT等级:[" + i + "]"));
      target.sendPackets((ServerBasePacket)new S_SystemMessage("\\fW血量:[" + msg2 + "]  //  魔力:[" + msg3 + "]"));
      target.sendPackets((ServerBasePacket)new S_SystemMessage("\\fU防御:[" + j + "]  //  魔防:[" + msg11 + "]"));
      target.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR力量:[" + s1 + "]  //  敏捷:[" + s2 + "]"));
      target.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR智力:[" + s3 + "]  //  体质:[" + s4 + "]"));
      target.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR精神:[" + s5 + "]  //  魅力:[" + s6 + "]"));
    } 
  }
  
  public void ItemName(L1PcInstance pc, String arg) {
    StringTokenizer st = new StringTokenizer(arg);
    String nameid = st.nextToken();
    int dropID = 0;
    try {
      dropID = Integer.parseInt(nameid);
    } catch (NumberFormatException e) {
      dropID = ItemTable.get().findItemIdByNameWithoutSpace1(nameid);
      if (dropID == 0) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("找不到您指定的道具名称"));
        return;
      } 
    } 
    if (dropID == 40308) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("【金币】几乎都会掉落"));
    } else {
      Connection con = null;
      PreparedStatement pstm = null;
      ResultSet rs = null;
      try {
        String blessed;
        L1Item item = ItemTable.get().getTemplate(dropID);
        if (item.getBless() == 1) {
          blessed = "";
        } else if (item.getBless() == 0) {
          blessed = "\\fR";
        } else {
          blessed = "\\fY";
        } 
        con = DatabaseFactory.get().getConnection();
        pstm = con.prepareStatement("SELECT mobId,min,max,chance FROM droplist WHERE itemId=?");
        pstm.setInt(1, dropID);
        rs = pstm.executeQuery();
        rs.last();
        int rows = rs.getRow();
        int[] mobID = new int[rows];
        double[] chance = new double[rows];
        String[] name = new String[rows];
        rs.beforeFirst();
        int i = 0;
        while (rs.next()) {
          mobID[i] = rs.getInt("mobId");
          chance[i] = rs.getInt("chance") / 10000.0D;
          i++;
        } 
        rs.close();
        pstm.close();
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\f=此物品:【" + blessed + item.getName() + "】掉落的怪物如下:"));
        int j = 0;
        while (j < mobID.length) {
          pstm = con.prepareStatement("SELECT name FROM npc WHERE npcid=?");
          pstm.setInt(1, mobID[j]);
          rs = pstm.executeQuery();
          while (rs.next())
            name[j] = rs.getString("name"); 
          rs.close();
          pstm.close();
          if (pc.isGm()) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fS怪物: " + name[j] + " 机率: " + chance[j] + "%"));
          } else {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fS怪物: " + name[j]));
          } 
          j++;
        } 
      } catch (Exception exception) {
      
      } finally {
        SQLUtil.close(rs, pstm, con);
      } 
    } 
  }
  
  private void KeyInEnemyName(L1PcInstance pc, String name) {
    if (!CharacterTable.doesCharNameExist(name)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU您输入的名称错误 资料库无此资料。"));
      pc.setKeyInEnemy(false);
    } else if (!pc.isInEnemyList(name)) {
      if (pc.getName().equals(name)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU不能添加自己为仇人"));
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU资料确认成功 !已经成功添加名单"));
        NewAutoPractice.get().AddEnemyList(pc, name);
        pc.setKeyInEnemy(false);
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        pc.setInEnemyList(name);
      } 
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU名称重复 请确认"));
      pc.setKeyInEnemy(false);
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
  
  private void KeyOutEnemyName(L1PcInstance pc, String name) {
    if (!CharacterTable.doesCharNameExist(name)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU您输入的名称错误 资料库无此资料。"));
      pc.setKeyOutEnemy(false);
    } else if (pc.isInEnemyList(name)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU资料确认成功 !已经成功从名单中移除"));
      NewAutoPractice.get().DeleteEnemyList(pc, name);
      pc.setKeyOutEnemy(false);
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
      pc.removeInEnemyList(name);
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU你的仇人名单内并无此人喔"));
      pc.setKeyOutEnemy(false);
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
  
  private void BadKeyInEnemyName(L1PcInstance pc, String name) {
    if (!CharacterTable.doesCharNameExist(name)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU您输入的名称错误 资料库无此资料。"));
      pc.setBadKeyInEnemy(false);
    } else if (!pc.isBadInEnemyList(name)) {
      if (pc.getName().equals(name)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU不能添加自己为黑名单"));
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU资料确认成功 !已经成功添加名单"));
        NewAutoPractice.get().AddBadEnemyList(pc, name);
        pc.setBadKeyInEnemy(false);
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        pc.setBadInEnemyList(name);
      } 
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU名称重复 请确认"));
      pc.setBadKeyInEnemy(false);
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
  
  private void BadKeyOutEnemyName(L1PcInstance pc, String name) {
    if (!CharacterTable.doesCharNameExist(name)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU您输入的名称错误 资料库无此资料。"));
      pc.setBadKeyOutEnemy(false);
    } else if (pc.isBadInEnemyList(name)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU资料确认成功 !已经成功从黑名单中移除"));
      NewAutoPractice.get().DeleteBadEnemyList(pc, name);
      pc.setBadKeyOutEnemy(false);
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
      pc.removeBadInEnemyList(name);
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU你的黑名单内并无此人喔"));
      pc.setBadKeyOutEnemy(false);
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
